// pages/createvent/createvent.js
const { $Message } = require('../../iview/base/index');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    datefrom: '2019-01-01',
    timefrom: '6:00',
    dateto: '2020-12-31',
    timeto: '22:00',
    picUploadFinish: false
  },

  inputChange(e) {
    var that = this
    switch (e.currentTarget.id) {
      case "name":
        {
          that.setData({
            name: e.detail.value
          })
        }
        break
      case "profile":
        {
          that.setData({
            profile: e.detail.value
          })
        }
        break
      case "place":
        {
          that.setData({
            place: e.detail.value
          })
        }
        break
    }
  },


  bindDatefromChange(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      datefrom: e.detail.value
    })
  },
  bindTimefromChange(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      timefrom: e.detail.value
    })
  },
  bindDatetoChange(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      dateto: e.detail.value
    })
  },
  bindTimetoChange(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      timeto: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      hostid: options.id
    })

  },

  uploadPic: function(e) {
    var that = this
    if (this.data.name == undefined) {
      $Message({
        content: '请先填写活动名称',
        type: 'default'
      })
    } else {
      wx.chooseImage({
        success: function(img) {
          wx.cloud.uploadFile({
            cloudPath: "eventimage/" + that.data.name + ".png",
            filePath: img.tempFilePaths[0]
          }).then(function(cldimg) {
            console.log(cldimg)
            that.setData({
              imgPath: cldimg.fileID,
              picUploadFinish: true
            })
          })
        }
      })
    }
  },

  commit: function(e) {
    var that = this
    if (this.data.name != undefined && this.data.place != undefined && this.data.profile != undefined) {
      var _event = {
        id: that.data.hostid,
        name: that.data.name,
        place: that.data.place,
        profile: that.data.profile,
        dtfrom: that.data.datefrom,
        tmfrom: that.data.timefrom,
        tmto: that.data.timeto,
        dtto: that.data.dateto,
        picture: that.data.imgPath,
        count: 0
      }

      wx.cloud.callFunction({
        name: "createEvent",
        data: {
          eventobj: _event
        }
      }).then(function(e) {
        console.log(e)
        $Message({
          content: '活动创建成功',
          type: "success"
        })
        setTimeout(function(){
          wx.navigateBack({
            delta: 1,
          })
        }, 1500)

      }).catch(function(err) {
        $Message({
          content: '活动信息上传错误，请联系开发者',
          type: 'error'
        })
      })
    } else {
      $Message({
        content: '请填写完整信息喔~',
        type: "defalut"
      });
    }
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})